#include "LinePainter.h"
#include "ui_LinePainter.h"
#include <QDebug>
LinePainter::LinePainter(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LinePainter)
{
    ui->setupUi(this);
    scene = new QGraphicsScene();
    ui->graphicsView->setScene(scene);
    line1 = new QGraphicsLineItem();
    border_rect.setRect(10,10,height()-10,width()-10);

    int height = this->height();
    QPen pen;
    pen.setColor(QColor(0, 0, 255, 100));
    pen.setWidth(3);
    line1->setPen( pen);
    line1->setLine(QLineF(QPointF(20,height/10),QPointF(20,height - height/10)));

    pen.setColor(QColor(255, 0, 0, 100));
    line2 = new QGraphicsLineItem();
    line2->setPen(pen);
    line2->setLine(QLineF(QPointF(10,height/10),QPointF(10,height - height/10)));
    scene->addItem(line1);
    scene->addItem(line2);

    m_timer = new QTimer();
    connect(m_timer,&QTimer::timeout,this,&LinePainter::slotUpdateLinePos);
    m_timer->start(100);
    // qDebug() << border_rect.bottomLeft() << border_rect.bottomRight() << border_rect.topLeft() << border_rect.topRight();
}

LinePainter::~LinePainter()
{
    delete ui;
}

void LinePainter::slotUpdateLinePos()
{
    QLineF l1 = line1->line();
    QLineF l2 = line2->line();

   {
       QPointF p1 = l1.p1();
       int x = p1.x() + 10;
       if (x > border_rect.topRight().x())
       {
           x = border_rect.topLeft().x() + (x - border_rect.topRight().x());
       }
       p1.setX(x);
       l1.setP1(p1);
   }
   {
       QPointF p2 = l2.p1();
       int x = p2.x() - 10;
       if (x < border_rect.topLeft().x())
       {
           x = border_rect.topRight().x() - abs(x);
       }
       p2.setX(x);
       l2.setP1(p2);
   }


   line1->setLine(l1);
   line2->setLine(l2);
}



